package com.enqualcomm.kids.activities;

import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.enqualcomm.kids.R;
import com.enqualcomm.kids.base.BaseActivity;
import com.enqualcomm.kids.bean.StringMessage;
import com.enqualcomm.kids.config.userdefault.AppDefault;
import com.enqualcomm.kids.config.userdefault.TerminalDefault;
import com.enqualcomm.kids.config.userdefault.UserTerminalDefault;
import com.enqualcomm.kids.mvp.NetworkModel;
import com.enqualcomm.kids.network.NetworkListener;
import com.enqualcomm.kids.network.SocketRequest;
import com.enqualcomm.kids.network.socket.request.SetLocModeParams;
import com.enqualcomm.kids.network.socket.request.TerminalConfigParams;
import com.enqualcomm.kids.network.socket.response.BasicResult;
import com.enqualcomm.kids.network.socket.response.QueryUserTerminalInfoResult;
import com.enqualcomm.kids.network.socket.response.TerminalConfigResult;
import com.enqualcomm.kids.network.socket.response.TerminallistResult;

import java.util.Locale;

import common.utils.PromptUtil;
import de.greenrobot.event.EventBus;

public class LocationModeActivity extends BaseActivity implements View.OnClickListener {

    private TerminallistResult.Terminal terminal;
    private TerminalConfigResult.Data config;
    private TerminalDefault terminalDefault;
    private NetworkModel networkModel;
    private String mWatchWorkMode;

    private RelativeLayout mExactlyRL;
    private RelativeLayout manuallyRL;
    private RelativeLayout mSavePowerRL;

    TextView mode_desc_1;
    TextView mode_desc_2;
    private ImageView mExactly_iv;
    private ImageView manually_iv;
    private ImageView mSavePower_iv;
    private AppDefault appDefault;
    private boolean isModified;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_location_mode);


        terminal = getTerminal();
        terminalDefault = new TerminalDefault(terminal.terminalid);
        config = terminalDefault.getConfig();
        networkModel = new NetworkModel();
        appDefault = new AppDefault();

        initView();
        syncConfig();
    }

    private void syncConfig() {
        //逗比需求
        networkModel.loadDataFromServer(new SocketRequest<TerminalConfigResult>(new TerminalConfigParams(appDefault.getUserkey(),terminal.terminalid),
                new NetworkListener<TerminalConfigResult>() {
                    @Override
                    public void onError(VolleyError error) {
                    }

                    @Override
                    public void onSuccess(TerminalConfigResult response) {
                        if (!modify && response.code == 0) {//如果用户有修改操作，就不需要更新了
                            config.workmode = mWatchWorkMode = response.result.workmode;
                            terminalDefault.setConfig(config);

                            String open_str = getString(R.string.location_mode_exactly_line1);
                            String close_str = getString(R.string.location_mode_manually_line1);
                            if (config.gps == 1) {
                                open_str = open_str + getString(R.string.location_mode_fence);
                                close_str = close_str + getString(R.string.location_mode_fence);
                            }
                            if (config.gps == 1) {
                                open_str = open_str + "、" + getString(R.string.location_mode_weather);
                                close_str = close_str + "、" + getString(R.string.location_mode_weather);
                            } else {
                                open_str = open_str + getString(R.string.location_mode_weather);
                                close_str = close_str + getString(R.string.location_mode_weather);
                            }
                           /* if (config.gps != 1) {
                                open_str = open_str + getString(R.string.location_mode_pedometer);
                                close_str = close_str + getString(R.string.location_mode_pedometer);
                            } else */if(config.pedometer == 1){
                                open_str = open_str + "、" + getString(R.string.location_mode_pedometer);
                                close_str = close_str + "、" + getString(R.string.location_mode_pedometer);
                            }

                            mode_desc_1.setText(open_str);
                            mode_desc_2.setText(close_str);
                            updateWorkmodeView();
                        }
                    }
                }));
    }

    private void updateWorkmodeView() {
        if (mWatchWorkMode.equals("2@1")||mWatchWorkMode.equals("1@1")||(mWatchWorkMode.equals("0@0"))) {
            mExactly_iv.setImageResource(R.drawable.mode_select);
            manually_iv.setImageResource(R.drawable.mode_no_select);
//            mSavePower_iv.setImageResource(R.drawable.mode_no_select);
        } else if (mWatchWorkMode.equals("4@0")) {
            manually_iv.setImageResource(R.drawable.mode_select);
            mExactly_iv.setImageResource(R.drawable.mode_no_select);
//            mSavePower_iv.setImageResource(R.drawable.mode_no_select);
        } else if (mWatchWorkMode.equals("3@10")) {
//            mSavePower_iv.setImageResource(R.drawable.mode_select);
            manually_iv.setImageResource(R.drawable.mode_no_select);
            mExactly_iv.setImageResource(R.drawable.mode_no_select);
        }
    }

    private void initView() {
        QueryUserTerminalInfoResult.Data info = new UserTerminalDefault(terminal.userterminalid).getInfo();
        if ("ZH".equals(Locale.getDefault().getLanguage().toUpperCase(Locale.getDefault()))) {
            setupTitleIcon(info,terminal.terminalid,terminal.userterminalid,getString(R.string.work_mode));
        }else {
            setupTitleIcon(info, terminal.terminalid, terminal.userterminalid, "Back");
        }

        findViewById(R.id.title_bar_left_iv).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        mExactlyRL = (RelativeLayout) findViewById(R.id.exactly_rl);
        manuallyRL = (RelativeLayout) findViewById(R.id.manually_rl);
        mSavePowerRL = (RelativeLayout) findViewById(R.id.save_power_rl);

        mExactly_iv = (ImageView) findViewById(R.id.exactly_iv);
        manually_iv = (ImageView) findViewById(R.id.manually_iv);
        mSavePower_iv = (ImageView) findViewById(R.id.save_power_iv);

        mExactlyRL.setOnClickListener(this);
        manuallyRL.setOnClickListener(this);
        mSavePowerRL.setOnClickListener(this);

        mWatchWorkMode = config.workmode;

        mode_desc_1 = (TextView) findViewById(R.id.mode_desc_1);
        mode_desc_2 = (TextView) findViewById(R.id.mode_desc_2);

        String open_str = getString(R.string.location_mode_exactly_line1);
        String close_str = getString(R.string.location_mode_manually_line1);
        if (config.gps == 1) {
            open_str = open_str + getString(R.string.location_mode_fence);
            close_str = close_str + getString(R.string.location_mode_fence);
        }
        if (config.gps == 1) {
            open_str = open_str + "、" + getString(R.string.location_mode_weather);
            close_str = close_str + "、" + getString(R.string.location_mode_weather);
        } else{
            open_str = open_str + getString(R.string.location_mode_weather);
            close_str = close_str + getString(R.string.location_mode_weather);
        }
/*        if (config.gps != 1&&config.pedometer == 1) {
            open_str = open_str + getString(R.string.location_mode_pedometer);
            close_str = close_str + getString(R.string.location_mode_pedometer);
        } else*/ if (config.pedometer == 1) {
            open_str = open_str + "、" + getString(R.string.location_mode_pedometer);
            close_str = close_str + "、" + getString(R.string.location_mode_pedometer);
        }
//        }

        mode_desc_1.setText(open_str);
        mode_desc_2.setText(close_str);
        updateWorkmodeView();
    }


    @Override
    protected void onStop() {
        super.onStop();
        networkModel.onStop();
    }


    private boolean modify;//如果同步config还没完成，用户又点击了按钮，这时同步过来的数据就有可能是错的

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            //精确定位-------1
            case R.id.exactly_rl:
                if ("2@1".equals(mWatchWorkMode)){
                    return;
                }
                if (mWatchWorkMode.equals("4@0")) {
                    manually_iv.setImageResource(R.drawable.mode_no_select);
                } else if (mWatchWorkMode.equals("3@10")) {
                    mSavePower_iv.setImageResource(R.drawable.mode_no_select);
                }
                mExactly_iv.setImageResource(R.drawable.mode_select);
                mWatchWorkMode = "2@1";//R06手表已经改成2@1
                break;

            //手动模式-------0
            case R.id.manually_rl:
                if ("4@0".equals(mWatchWorkMode)){
                    return;
                }
                if (mWatchWorkMode.equals("2@1")) {
                    mExactly_iv.setImageResource(R.drawable.mode_no_select);
                } else if (mWatchWorkMode.equals("3@10")) {
                    mSavePower_iv.setImageResource(R.drawable.mode_no_select);
                }
                manually_iv.setImageResource(R.drawable.mode_select);
                mWatchWorkMode = "4@0";
                break;
            //省电定位-------10
            case R.id.save_power_rl:
                if ("3@10".equals(mWatchWorkMode)){
                    return;
                }
                if (mWatchWorkMode.equals("2@1")) {
                    mExactly_iv.setImageResource(R.drawable.mode_no_select);
                } else if (mWatchWorkMode.equals("4@0")) {
                    manually_iv.setImageResource(R.drawable.mode_no_select);
                }
                mSavePower_iv.setImageResource(R.drawable.mode_select);
                mWatchWorkMode = "3@10";
                break;
        }

        modify = true;
        SetLocModeParams params = new SetLocModeParams(appDefault.getUserkey(),appDefault.getUserid(),terminal.terminalid, mWatchWorkMode);
        updateLocationMode(params);
    }

    private void updateLocationMode(SetLocModeParams params) {
        showProgress();
        networkModel.loadDataFromServer(new SocketRequest<BasicResult>(params, new NetworkListener<BasicResult>() {
            @Override
            public void onError(VolleyError error) {
                hideProgress();
                PromptUtil.toast(getApplicationContext(),R.string.app_no_connection);
            }

            @Override
            public void onSuccess(BasicResult response) {
                hideProgress();
                if (response.code==0){
                    PromptUtil.toast(getApplicationContext(), getString(R.string.location_promote));
                    config.workmode = mWatchWorkMode;
                    terminalDefault.setConfig(config);
                    isModified = true;
                }
            }
        }));

    }

    @Override
    protected void onDestroy() {
        if (isModified){
            EventBus.getDefault().post(new StringMessage(StringMessage.LOCATION_MODE));
        }
        super.onDestroy();
    }
}
